/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package matrici;

public class PageRank {
    
    public PageRank(){
        double A[][] = {{1,0,1},{1,1,0},{1,1,1}};
        System.out.println(OperazioniSuMatrici.stampaMatrice(A));
        System.out.println(OperazioniSuMatrici.stampaMatrice(pageRank(A, 0.0001, 0.15)));     
    }

    public double[] pageRank(double[][] matriceDiAdiacenza, double epsilon, double dumpingFactor) {
        int n = matriceDiAdiacenza.length;
        int m = matriceDiAdiacenza[0].length;
        if (n != m) {
            return null;
        } else {
            double[][] P = OperazioniSuMatrici.normalizzaRigheMatrice(matriceDiAdiacenza);
            P = OperazioniSuMatrici.transpose(P);
            double[] x = new double[n];
            for (int i = 0; i < n; i++) {
                x[i] = 1. / n;
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    P[i][j] = P[i][j] * (1 - dumpingFactor) + dumpingFactor / n;
                }
            }
            System.out.println(OperazioniSuMatrici.stampaMatrice(P));
            double[] x1 = OperazioniSuMatrici.product(P, x);
            while (epsilon < OperazioniSuMatrici.norma(OperazioniSuMatrici.difference(x, x1), 1)) {
                x = x1;
                x1 = OperazioniSuMatrici.product(P, x);
            }
            return x1;
        }
    }
    
    public static void main(String [] args){
        PageRank pageRank = new PageRank();
    }
}
